package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.PaymentInfo;
import com.mju.service.IPaymentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/paymentInfo")
@Api(tags = "支付信息服务")
public class PaymentInfoController extends BaseController {
    @Autowired private IPaymentInfoService paymentInfoService;

    @ApiOperation("记录电脑端支付信息")
    @PostMapping("/createPaymentInfo")
    public JsonResult createPaymentInfo(@RequestParam Map<String, String> params){
        log.info("开始记录电脑端支付信息...");
        paymentInfoService.createPaymentInfo(params);
        return new JsonResult(OK,"记录支付信息成功");
    }

    @ApiOperation("记录移动端支付信息")
    @PostMapping("/createPaymentInfoForMobileTerminal")
    public JsonResult createPaymentInfoForMobileTerminal(@RequestParam Map<String, String> params){
        log.info("开始记录移动端支付信息...");
        paymentInfoService.createPaymentInfo(params);
        return new JsonResult(OK,"记录支付信息成功");
    }

    @ApiOperation("无条件查询所有支付信息")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<PaymentInfo>> queryAllPaymentInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        IPage<PaymentInfo> data = paymentInfoService.queryAllPaymentInfo(currentPage, pageSize);
        return new JsonResult<>(OK,data);
    }

    @ApiOperation("条件查询所有支付信息,条件最多包含订单号，支付宝交易订单号，交易金额，交易状态")
    @PostMapping("/conQueryAllPaymentInfo")
    public JsonResult<IPage<PaymentInfo>> conQueryAllPaymentInfo(
            @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize,@RequestBody PaymentInfo paymentInfo){
        IPage<PaymentInfo> data = paymentInfoService.conQueryAllPaymentInfo(currentPage, pageSize,paymentInfo);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > data.getPages()){
            data = paymentInfoService.conQueryAllPaymentInfo((int)data.getPages(), pageSize,paymentInfo);
        }
        return new JsonResult<>(OK,data);
    }
}
